// miniprogram/pages/admin/roomManager.js
import moment from "../../libs/moment.js"
const regeneratorRuntime = require('../../libs/runtime')
const {
    $Message
} = require('../../components/base/index.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        roomUnitList: "",
        startDate: "",
        showAction: false,
        toggle: false,
        actions: [{
            name: '删除',
            color: '#ed3f14'
        }],
        currentRoom: "",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getRoomList();
    },
    selectStartDate(e) {
        this.setData({
            startDate: e.detail.value
        })
        this.getRoomList(true)
    },
    showAll() {
        this.getRoomList(false)
    },
    async getRoomList(isFilter) {
        const db = wx.cloud.database()
        let roomUnitList = null
        await db.collection('room_unit_list').get().then(res => {
            roomUnitList = res.data
        }).catch(err => {
            console.log(err)
        })
        if (!isFilter) {
            this.setData({
                roomUnitList: roomUnitList
            })
            return
        }
        for (let i = 0; i < roomUnitList.length; i++) {
            let roomDateList = JSON.parse(roomUnitList[i].roomOrderDateList);
            let orderedNum = 0;
            let compareDate = this.data.startDate
            for (let i = 0; i < roomDateList.length; i++) {
                if (roomDateList[i] == undefined || roomDateList[i] == null || roomDateList[i] == 'null') { //如果该房间没有被人订过
                    orderedNum++
                    continue
                } else {
                    //如果有预订记录 则判断离店时间
                    if (moment(compareDate).isAfter(orderList[i]['endDate']) || moment(orderList[i]['endDate']).isSame(compareDate)) {
                        orderedNum++;
                        continue;
                    } else {
                        continue
                    }
                }
            }
            roomUnitList[i].orderedNum = orderedNum;
        }
        this.setData({
            roomUnitList: roomUnitList
        })
    },
    editRoom({
        currentTarget
    }) {
        let item = currentTarget.dataset.item
        let itemStr = JSON.stringify(item)
        wx.navigateTo({
            url: `/pages/admin/editRoom?roomItem=${itemStr}`
        })
    },
    deleteRoom(e) {
        this.setData({
            showAction: true,
            currentRoom: e.currentTarget.dataset.item
        });
    },
    handleCancel() {
        //关闭 actionsheet
        this.setData({
            showAction: false,
            toggle: this.data.toggle ? false : true
        });
    },
    //执行删除
    async handleClickItem(e) {
        let index = e.detail.index
        if (index == 0) {
            await wx.cloud.callFunction({
                name: 'deleteRoom',
                data: {
                    room: this.data.currentRoom
                }
            }).then(res => {
                if (res.result.stats.removed) {
                    $Message({
                        content: '删除成功'
                    });
                    this.handleCancel();
                }
            }).catch(err => {
                console.log(err)
            })
        }
    },
})